package com.iman.Confucius.web.student;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.iman.Confucius.domain.SessionBean;
import com.iman.Confucius.domain.User;
import com.iman.Confucius.domain.UserSInfo;
import com.iman.Confucius.mappers.models.DrmBuyOrder;
import com.iman.Confucius.service.MongoMessageDetailService;
import com.iman.Confucius.service.UserService;
import com.iman.Confucius.service.student.ClassService;
import com.iman.Confucius.service.student.CourseService;
import com.iman.Confucius.service.student.DrmBuyOrderDetailMapper;
import com.iman.Confucius.service.student.DrmBuyOrderMapper;
import com.iman.Confucius.service.student.NoticeService;
import com.iman.Confucius.vo.student.ClassVo;
import com.iman.Confucius.vo.student.NoticeVo;
import com.iman.Confucius.vo.student.StudyDmsgVo;
import com.iman.Confucius.vo.student.StudyRemarkVo;
import com.iman.Confucius.web.BaseController;
import com.iman.common.Constants;
import com.iman.common.support.Page;

 

/**
 * *************************************************************************
 * @文件名称: StudentHomeController.java
 *
 * @包路径  : com.iman.Confucius.web.student 
 *				 
 * @版权所有:   TSW   科技有限公司 (C) 2014
 *
 * @类描述:  初始化的首页
 * 
 * @创建人:   wangxiaolei  
 *
 * @创建时间: 2014年12月18日 - 上午1:53:17 
 *
 * @修改记录:
   -----------------------------------------------------------------------------------------------
             时间						|		修改人		|		修改的方法		|		修改描述                                                                
   -----------------------------------------------------------------------------------------------
							|					|					|                                       
   ----------------------------------------------------------------------------------------------- 	
 
 **************************************************************************
 */
@Controller
@RequestMapping(value="/student/*")
public class StudentHomeController  extends BaseController {
	
	@Autowired
	private MongoMessageDetailService  mongoMessageDetailService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private ClassService classServiceImpl;
	@Autowired
	private NoticeService noticeService;
	
	@Autowired
	private  UserService	userService;
	
	@Autowired
	private DrmBuyOrderMapper drmBuyOrderMapperService;
	
	@Autowired
	private DrmBuyOrderDetailMapper drmBuyOrderDetailMapperService;
	
//	private static enum DMSG_SUB {
//		BUY(0), START(1), LEARNING(2), END(3);
//		private int tabId = 0;
//		DMSG_SUB(int tabId) {
//			this.tabId = tabId;
//		}
//	}
	/**
	 * 
	 *  Function:
	 *  功能说明：
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2014年12月16日 下午3:00:09
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @return
	 */
	@RequestMapping(value="/init")
	public ModelAndView initjspParamter(HttpServletRequest request) throws Exception {
		return initMainMycourse(request, "1", "10");
	}
	
	/**
	 * @description 获得所有课程列表
	 * @param request
	 * @param pageNo
	 * @param pageSize
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月24日
	 */
	@RequestMapping(value="/mainMycourse/{pageNo}/{pageSize}")
	public  ModelAndView initMainMycourse(HttpServletRequest request,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize) throws Exception {
		if(!StringUtils.isNumeric(pageNo)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(pageSize)){
			throw new IllegalArgumentException("无效分页参数");
		}
		
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		Map<String, Object> model = new HashMap<String, Object>();
		//初始化最新学习的课程
		Page<StudyRemarkVo> recentRemarkList = new Page<StudyRemarkVo>();
		Map<String, Object> mapRecentCourse = new HashMap<String, Object>();
		mapRecentCourse.put("userId", userId);
		mapRecentCourse.put(Constants.PAGE_INDEX_KEY, 1);
		mapRecentCourse.put(Constants.PAGE_SIZE_KEY, 4);
		recentRemarkList = courseService.getStudyRemarkList(mapRecentCourse);
		model.put("recentRemarkList", recentRemarkList);
		
		//初始化公告
		Map<String, Object> noticeParam = new HashMap<String, Object>();
		noticeParam.put("ps", 4);
		noticeParam.put("pn", 1);
		Page<NoticeVo> noticeList = noticeService.getNoticePage(noticeParam);
		model.put("noticeList", noticeList);
		//我的班级
		Map<String, Object> mapMyClass = new HashMap<String, Object>();
		mapMyClass.put("userid", userId); 
		List<ClassVo> vos = classServiceImpl.getStudentClass(mapMyClass);
		if(vos!=null&&vos.size()>0){
			ClassVo recentClass = vos.get(0);
			model.put("recentClass", recentClass);
			//班主任信息
			Map<String, Object> mapMyTeacher = new HashMap<String, Object>();
			mapMyTeacher.put("classid", recentClass.getId());
			List<User> classTeacher = classServiceImpl.getClassTeacher(mapMyTeacher);
			if(classTeacher!=null&&classTeacher.size()>0){
				model.put("classTeacher", classTeacher.get(0));
			}
			//初始化班级成员
			Map<String, Object> mapClassmate = new HashMap<String, Object>();
			mapClassmate.put("classid", recentClass.getId()); 
			mapClassmate.put("pn", 1);
			mapClassmate.put("ps", 8);
			Page<User> classmatePage  =classServiceImpl.getStudentsPage(mapClassmate);
			model.put("classmatePage", classmatePage);
		}
		
		//初始化学习动态
		Page<StudyDmsgVo> studyDmsgList = new Page<StudyDmsgVo>();
		Map<String, Object> mapDmsg = new HashMap<String, Object>();
		mapDmsg.put("userId", userId);
		mapDmsg.put(Constants.PAGE_INDEX_KEY, Integer.parseInt( pageNo ));
		mapDmsg.put(Constants.PAGE_SIZE_KEY, Integer.parseInt( pageSize ));
		studyDmsgList = courseService.getAllStudyDmsgList(mapDmsg);
		model.put("studyDmsgList", studyDmsgList);
		
		ModelAndView view =null;
		view = new ModelAndView("student/main",model);
		//等级信息
		initStudentInfo(request,view);
		putResourceSrc(  view);
		
		return view;
	}
	
	/**
	 *  Function:
	 *	 使用说明：获取用户信息  和  用户等级信息
	 *  @author  wangxiaolei  DateTime 2015年1月10日 下午11:02:29
	 *	返回类型: void    
	 *  @param request
	 *  @param model
	 */
	private void initStudentInfo(HttpServletRequest request,ModelAndView model){
		SessionBean  session = super.getSession( request) ;
		UserSInfo usinfo = this.userService.getUserSInfo( session.getUser() .getUserId());
		model.addObject( "userSinfo", usinfo);
		model.addObject("userinfo",  session.getUser() );
	}
	/**
	 * 
	 *  Function:
	 *  功能说明：
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2014年12月16日 下午3:00:09
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @return
	 */
	@RequestMapping(value="/course/init")
	public ModelAndView initCourse(HttpServletRequest request){
		Map model = new HashMap();
		model.put("user", "测试传递变量" );
		 
		ModelAndView view =null;
		 
		view = new ModelAndView("student/main",model);
		
		
		putResourceSrc(  view);
		
		return view;
	}
	
	/**
	 * 个人空间
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/per")
	public ModelAndView per(HttpServletRequest request){
		Map model = new HashMap();
		 
		ModelAndView view =null;
		 
		view = new ModelAndView("student/my_per",model);
		
		
		putResourceSrc(view);
		
		return view;
	}
	
	/**
	 * 订单详情
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/order")
	public ModelAndView order(HttpServletRequest request){
		Map model = new HashMap();
		 
		ModelAndView view =null;
		
		SessionBean  session = super.getSession( request) ;
		UserSInfo usinfo = this.userService.getUserSInfo( session.getUser() .getUserId());
		
		String orderSt = request.getParameter("orderSt");
		//查询订单
		List<DrmBuyOrder> list =  this.drmBuyOrderMapperService.selectAll(usinfo.getUser_id(),orderSt);
		model.put("order_list", list) ;
		
		view = new ModelAndView("student/my_order",model);
		
		
		putResourceSrc(view);
		
		return view;
	}
	
	
	/**
	 * 订单详情
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/order/detail")
	public ModelAndView ordeDetailr(HttpServletRequest request){
		Map model = new HashMap();
		 
		ModelAndView view =null;
		
		SessionBean  session = super.getSession( request) ;
		UserSInfo usinfo = this.userService.getUserSInfo( session.getUser() .getUserId());
		
		String orderSt = request.getParameter("orderSt");
		//查询订单
		List<DrmBuyOrder> list =  this.drmBuyOrderMapperService.selectAll(usinfo.getUser_id(),orderSt);
		model.put("order_list", list) ;
		
		view = new ModelAndView("student/my_order_detail",model);
		
		
		putResourceSrc(view);
		
		return view;
	}

}
